package com.twu.biblioteca.util;

import com.twu.biblioteca.Library;
import com.twu.biblioteca.model.Account;
import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Movie;
import com.twu.biblioteca.operation.*;
import com.twu.biblioteca.service.BookService;
import com.twu.biblioteca.service.MovieService;
import com.twu.biblioteca.service.UserService;

import java.util.ArrayList;
import java.util.List;

public class Configuration {
    private static List<Book> books = new ArrayList<Book>();

    private static List<Movie> movies = new ArrayList<Movie>();

    private static List<Account> accounts = new ArrayList<Account>();
    private static Library library = new Library();
    private static UserService userService = new UserService();
    private static BookService bookService = new BookService();
    private static MovieService movieService = new MovieService();

    static {
        library = initLibrary();
        userService = initUserService(library);
        bookService = initBookService(library, userService);
        movieService = initMovieService(library, userService);
    }

    public Operation initListMoviesOperation() {
        ListMoviesOperation listMoviesOperation = new ListMoviesOperation();
        listMoviesOperation.setMovieService(movieService);

        return listMoviesOperation;
    }

    public Operation initViewUserOperation() {
        ViewUserOperation viewUserOperation = new ViewUserOperation();
        viewUserOperation.setUserService(userService);
        return viewUserOperation;
    }

    public Operation initLogoutSystemOperation() {
        return new LogoutSystemOperation();
    }

    public Operation initLoginSystemOperation() {
        LoginSystemOperation loginSystemOperation = new LoginSystemOperation();
        loginSystemOperation.setUserService(userService);
        return loginSystemOperation;
    }

    public Operation initListBooksOperation() {
        ListBooksOperation listBooksOperation = new ListBooksOperation();
        listBooksOperation.setBookService(bookService);
        return listBooksOperation;
    }

    public Operation initReturnMovieOperation() {
        ReturnMovieOperation returnMovieOperation = new ReturnMovieOperation();
        returnMovieOperation.setMovieService(movieService);
        return returnMovieOperation;
    }

    public ReturnBookOperation initReturnBookOperation() {

        ReturnBookOperation returnBookOperation = new ReturnBookOperation();
        returnBookOperation.setBookService(bookService);
        return returnBookOperation;
    }

    public CheckMovieOperation initCheckMovieOperation() {
        CheckMovieOperation checkMovieOperation = new CheckMovieOperation();
        checkMovieOperation.setMovieService(movieService);
        return checkMovieOperation;
    }

    public CheckBookOperation initCheckBookOperation() {
        CheckBookOperation checkBookOperation = new CheckBookOperation();
        checkBookOperation.setBookService(bookService);
        return checkBookOperation;
    }

    private static MovieService initMovieService(Library library, UserService userService) {
        movieService.setLibrary(library);
        movieService.setUserService(userService);
        return movieService;
    }

    public static BookService initBookService(Library library, UserService userService) {
        bookService.setLibrary(library);
        bookService.setUserService(userService);
        return bookService;
    }

    public static UserService initUserService(Library library) {
        userService.setLibrary(library);
        return userService;
    }

    public static Library initLibrary() {
        library.initBooks(initBooks());
        library.initMovies(initMovies());
        library.initAccounts(initAccounts());
        return library;
    }

    private static List<Account> initAccounts() {

        accounts.add(new Account("u11-1222", "123456", "danchen", "892098490@qq.com", "15107114547"));
        return accounts;
    }

    private static List<Movie> initMovies() {
        movies.add(new Movie(1, "moivename", "2013-6", "AnLi", 8));
        movies.add(new Movie(2, "moivename", "2013-6", "AnLi", 8));
        movies.add(new Movie(3, "moivename", "2013-6", "AnLi", 5));
        movies.add(new Movie(4, "moivename", "2013-6", "AnLi", 0));

        return movies;
    }

    public static List<Book> initBooks() {
        books.add(new Book(1, "The weakness of human nature", "David", "2014-7-12"));
        books.add(new Book(2, "The weakness of human nature", "Jhona", "2014-7-12"));
        books.add(new Book(3, "The weakness of human nature", "Luciy", "2014-7-12"));
        books.add(new Book(4, "The weakness of human nature", "Janey", "2014-7-12"));
        return books;
    }
}
